package com.example.my_helper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class MyMessageAdapter extends BaseAdapter {

    private ArrayList<MyMqttMessage> mData;
    private Context mContext;

    public MyMessageAdapter() {

    }


    public MyMessageAdapter(ArrayList<MyMqttMessage> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @SuppressLint({"ViewHolder", "SetTextI18n"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if(convertView != null){
            viewHolder = (ViewHolder)convertView.getTag();
        }else{
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.mqtt_message_list_item, parent, false);
            viewHolder.topic = convertView.findViewById(R.id.mqtt_message_topic_label);
            viewHolder.qos = convertView.findViewById(R.id.mqtt_message_qos_label);
            viewHolder.time = convertView.findViewById(R.id.mqtt_message_time_label);
            viewHolder.message = convertView.findViewById(R.id.mqtt_message_label);
            viewHolder.id = convertView.findViewById(R.id.mqtt_message_id_label);
            convertView.setTag(viewHolder);
        }

        viewHolder.topic.setText(mData.get(position).topic);
        viewHolder.qos.setText("Qos:"+mData.get(position).qos);
        //viewHolder.retained.setText(mData.get(position).retained ? "Retained" : "Not Retained");
        viewHolder.message.setText(mData.get(position).message);
        viewHolder.id.setText(""+mData.get(position).id);
        viewHolder.time.setText(mData.get(position).time);
        return convertView;
    }

    /** 创建 ViewHolder */
    class ViewHolder{
        TextView topic, qos, retained, message, time, id;
    }

}
